package com.sn.system.common.validator.validators;

import com.sn.system.common.utils.CheckUtils;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class LongLengthValidator extends FieldValidatorSupport{

	private String maxLength;
	private String minLength;
	
	public void validate(Object object) throws ValidationException {
		  String fieldName = getFieldName();
	        Object value = this.getFieldValue(fieldName, object);        
	        // XW-375 - must be a string
	        if (!(value instanceof Long)) {
	            return;
	        }

	        // string must not be empty
	        String str = value.toString();
	        if (str.length() == 0) {
	            return;
	        }
	        
	        if(CheckUtils.isNumber(getMaxLength())){
	        	 if(str.length()>Integer.parseInt(getMaxLength())){
	             	addFieldError(getFieldName(), object);
	             	return;
	             }
	        }
	        if(CheckUtils.isNumber(getMinLength())){
	        	 if(str.length()<Integer.parseInt(getMaxLength())){
	             	addFieldError(getFieldName(), object);
	             	return;
	             }
	        }
		
	}

	public String getMaxLength() {
		return maxLength;
	}

	public void setMaxLength(String maxLength) {
		this.maxLength = maxLength;
	}

	public String getMinLength() {
		return minLength;
	}

	public void setMinLength(String minLength) {
		this.minLength = minLength;
	}
	
	

}
